package com.tjmc.company.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjmc.company.api.entity.AutoRefreshLogEntity;
import com.tjmc.company.api.entity.CompanyInfoEntity;
import com.tjmc.company.api.entity.JobInfoEntity;
import com.tjmc.company.api.enums.JobStatusEnum;
import com.tjmc.company.api.enums.JobTopTypeEnum;
import com.tjmc.company.api.vo.JobDetailVO;
import com.tjmc.company.api.vo.JobTopVO;
import com.tjmc.company.mapper.JobInfoMapper;
import com.tjmc.company.service.AutoRefreshDurationConfService;
import com.tjmc.company.service.AutoRefreshLogService;
import com.tjmc.company.service.CompanyInfoService;
import com.tjmc.company.service.JobInfoService;
import com.tjmc.recruit.admin.api.entity.PlatformUserInfoEntity;
import com.tjmc.recruit.admin.api.enums.PlatUserTypeEnum;
import com.tjmc.recruit.admin.api.feign.RemoteUserService;
import com.tjmc.recruit.common.core.constant.CommonConstants;
import com.tjmc.recruit.common.core.constant.SecurityConstants;
import com.tjmc.recruit.common.core.exception.CheckedException;
import com.tjmc.recruit.common.core.util.R;
import com.tjmc.recruit.common.core.util.RedisUtils;
import com.tjmc.recruit.common.security.util.SecurityUtils;
import com.tjmc.recruit.finance.api.dto.AccountChangeDTO;
import com.tjmc.recruit.finance.api.entity.MemberCompanyEquityConfEntity;
import com.tjmc.recruit.finance.api.entity.PlatformUserAccountEntity;
import com.tjmc.recruit.finance.api.entity.UsageRecordsEntity;
import com.tjmc.recruit.finance.api.enums.ChangeMehtodEnum;
import com.tjmc.recruit.finance.api.enums.MemberStatusEnum;
import com.tjmc.recruit.finance.api.enums.UsageServiceTypeEnum;
import com.tjmc.recruit.finance.api.enums.UsageTypeEnum;
import com.tjmc.recruit.finance.api.feign.MemberInfoService;
import com.tjmc.recruit.finance.api.feign.RemoteAccountService;
import com.tjmc.recruit.finance.api.feign.RemoteUsageRecordsService;
import com.tjmc.recruit.finance.api.vo.MemberInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 职位信息表
 *
 * @author recruit
 * @date 2024-11-14 17:07:26
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class JobInfoServiceImpl extends ServiceImpl<JobInfoMapper, JobInfoEntity> implements JobInfoService {
    private final RedisTemplate<String, Object> redisTemplate;
    private final RemoteAccountService remoteAccountService;
    private final RemoteUsageRecordsService remoteUsageRecordsService;
    private final MemberInfoService memberInfoService;
    private final CompanyInfoService companyInfoService;
    private final AutoRefreshLogService autoRefreshLogService;
    private final RemoteUserService remoteUserService;

    //职位key
    public static final String JOB_REFRESH_KEY = "jobs:to_refresh";
    public static final String JOB_REFRESH_INFO_PREFIX = "job:refresh:";

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R top(JobTopVO jobTopVO) {
        // 1. 查询职位信息
        JobInfoEntity jobInfo = getById(jobTopVO.getJobId());
        if (jobInfo == null) {
            return R.failed("未查询到职位信息");
        }

        // 3. 变更账户数据
        AccountChangeDTO accountChangeDTO = new AccountChangeDTO();
        accountChangeDTO.setAccountId(jobTopVO.getAccountId());
        accountChangeDTO.setChangeMethod(ChangeMehtodEnum.SUBTRACT.name());
        accountChangeDTO.setChangeValue(jobTopVO.getECoin());
        accountChangeDTO.setUsageType(UsageTypeEnum.E_COIN);
        R r = remoteAccountService.changeAccountInfo(accountChangeDTO, SecurityConstants.FROM_IN);
        if (r.getCode() != CommonConstants.SUCCESS) {
            return R.failed(r.getMsg());
        }
        // 4. 新增消费记录
        UsageRecordsEntity usageRecordsEntity = getUsageRecords(jobTopVO, jobInfo);
        R add = remoteUsageRecordsService.add(usageRecordsEntity, SecurityConstants.FROM_IN);
        if (add.getCode() != CommonConstants.SUCCESS) {
            throw new CheckedException("新增消费记录数据失败");
        }

        // 2. 更新职位信息
        if (StrUtil.isBlank(jobTopVO.getTopType().name())) {
            return R.failed("置顶类型不能为空");
        }
        if (jobTopVO.getTopType().equals(JobTopTypeEnum.TOP)) {
            jobInfo.setTopDuration(String.valueOf(DateUtil.offsetDay(new Date(), jobTopVO.getTopDuration())));
        } else {
            jobInfo.setSuperTopDuration(String.valueOf(DateUtil.offsetDay(new Date(), jobTopVO.getTopDuration())));
        }
        boolean b = updateById(jobInfo);
        if (!b) {
            return R.failed("职位信息更新失败");
        }
        return R.ok(Boolean.TRUE);
    }

    @Override
    public Page<JobInfoEntity> getRecommendJobPage(Page page, String jobIntent) {
        // 1. 根据岗位信息匹配职位列表
        LambdaQueryWrapper<JobInfoEntity> wrapper = new LambdaQueryWrapper<JobInfoEntity>()
                .like(StrUtil.isNotBlank(jobIntent), JobInfoEntity::getJobName, jobIntent)
                .eq(JobInfoEntity::getStatus, JobStatusEnum.PUBLISHED)
                .orderByDesc(JobInfoEntity::getUpdateAt);
        return this.page(new Page<>(page.getCurrent(), page.getSize()), wrapper);
    }

    @Override
    public Page<JobInfoEntity> getJobPage(Page page) {
        //推广时间、置顶时间、修改时间
        Page<JobInfoEntity> objectPage = new Page<>(page.getCurrent(), page.getSize());
        return baseMapper.getJobPage(objectPage);
    }

    @Override
    public Page<JobInfoEntity> selectTopJobPage(Page page, Long companyId) {
        return baseMapper.selectTopJobPage(page, companyId);
    }

    @Override
    public R saveJobInfo(JobInfoEntity jobInfo) {
        //新增职位前需判断企业状态
        CompanyInfoEntity companyInfo = companyInfoService.getById(jobInfo.getCompanyId());
        if (companyInfo == null) {
            return R.failed("企业不存在");
        }
        if (companyInfo.getCertificationStatus().equals("0")) {
            return R.failed("企业未认证，不能发布职位");
        }
        // 1. 查询企业账户可发布数量
        String name = SecurityUtils.getUser().getName();
        R<PlatformUserInfoEntity> platformedUser = remoteUserService.platformUser(name, SecurityConstants.FROM_IN);
        PlatformUserInfoEntity platformUserInfo = platformedUser.getData();
        if (platformUserInfo == null) {
            return R.failed("未查询到当前用户信息");
        }
        //账户职位发布数
        R<Long> remainNum = getRemainNum();
        if (remainNum.getCode() == CommonConstants.FAIL){
            return R.failed(remainNum.getMsg());
        }
        Long remainNumData = remainNum.getData();
        //判断是否需要查询会员
        if (remainNumData == 0) {
            //如果达到上限，则使用E币
            //发布所需E币
            BigDecimal publishECoinAmount = jobInfo.getPublishECoinAmount();
            if (publishECoinAmount.compareTo(BigDecimal.ZERO) <= 0) {
                return R.failed("已达到发布数量上限");
            }
            //子账号逻辑
            String accountId;
            if (platformUserInfo.getAccountType().equals(PlatUserTypeEnum.MAIN)) {
                accountId = String.valueOf(platformUserInfo.getId());
            } else {
                // 取创建账号
                accountId = platformUserInfo.getCreateBy();
            }
            R<PlatformUserAccountEntity> accountInfo = remoteAccountService.getAccountInfo(accountId, SecurityConstants.FROM_IN);
            if (accountInfo.getCode() != CommonConstants.SUCCESS) {
                return R.failed("查询用户账户信息失败");
            }
            //账户E币余额
            BigDecimal accountECoinNum = accountInfo.getData().getECoinNum();
            if (accountECoinNum.compareTo(publishECoinAmount) < 0) {
                return R.failed("账户余额不足，无法发布职位");
            }
            //账户扣除
            AccountChangeDTO accountChangeDTO = new AccountChangeDTO();
            accountChangeDTO.setAccountId(accountInfo.getData().getAccountId());
            accountChangeDTO.setChangeMethod(ChangeMehtodEnum.SUBTRACT.name());
            accountChangeDTO.setChangeValue(publishECoinAmount);
            accountChangeDTO.setUsageType(UsageTypeEnum.E_COIN);
            R r = remoteAccountService.changeAccountInfo(accountChangeDTO, SecurityConstants.FROM_IN);
            if (r.getCode() != CommonConstants.SUCCESS) {
                return R.failed(r.getMsg());
            }
        }
        //设置默认职位发布状态：待审核
        jobInfo.setStatus(JobStatusEnum.REVIEWED);
        jobInfo.setStatusDesc(JobStatusEnum.REVIEWED.getDescription());
        save(jobInfo);
        // 初始化职位的刷新信息到Redis
        if (jobInfo.getAutoRefreshDuration() != 0) {
            initializeJobRefreshInfo(getById(jobInfo));
        }
        return R.ok(Boolean.TRUE);
    }


    @Override
    public Integer getPublishNumByMember(PlatformUserInfoEntity platformUserInfo) {
        R<MemberInfoVO> memberInfoVOR = memberInfoService.getAccountInfo(platformUserInfo.getId(), SecurityConstants.FROM_IN);
        if (memberInfoVOR.getCode() != CommonConstants.SUCCESS) {
            log.info(memberInfoVOR.getMsg());
            return 0;
        }
        MemberInfoVO infoVO = memberInfoVOR.getData();
        if (infoVO == null) {
            log.info("会员信息为空");
            return 0;
        }
        if (!MemberStatusEnum.ACTIVE.equals(infoVO.getStatus())) {
            log.info("会员未激活");
            return 0;
        }
        MemberCompanyEquityConfEntity companyEquityConf = infoVO.getCompanyEquityConf();
        if (companyEquityConf == null) {
            log.info("会员权益信息为空");
            return 0;
        }
        //会员权益：职位发布数
        return companyEquityConf.getJobPublishDayNum();
    }

    /**
     * 查询剩余发布数
     * @return
     */
    @Override
    public R<Long> getRemainNum() {
        // 1. 查询用户
        String name = SecurityUtils.getUser().getName();
        R<PlatformUserInfoEntity> platformedUser = remoteUserService.platformUser(name, SecurityConstants.FROM_IN);
        if (platformedUser.getCode() != CommonConstants.SUCCESS) {
            return R.failed(platformedUser.getMsg());
        }
        // 2.查询账户职位发布总数
        PlatformUserInfoEntity platformUserInfo = platformedUser.getData();
        if (platformUserInfo == null) {
            return R.failed("未查询到用户信息");
        }
        String accountId;
        if (platformUserInfo.getAccountType().equals(PlatUserTypeEnum.MAIN)) {
            accountId = String.valueOf(platformUserInfo.getId());
        } else {
            accountId = String.valueOf(platformUserInfo.getCreateBy());
        }
        R<PlatformUserAccountEntity> accountInfo = remoteAccountService.getAccountInfo(accountId, SecurityConstants.FROM_IN);
        if (accountInfo.getCode() != CommonConstants.SUCCESS) {
            return R.failed(accountInfo.getMsg());
        }
        //账户发布数
        Integer publishJobNum = accountInfo.getData().getPublishJobNum();
        Integer publishNumByMember = getPublishNumByMember(platformUserInfo);
        //3 .统计已发布职位
        long count = count(new LambdaQueryWrapper<JobInfoEntity>()
                .eq(JobInfoEntity::getCompanyId, platformUserInfo.getCompanyId())
                .eq(JobInfoEntity::getStatus, JobStatusEnum.PUBLISHED));
        // 4. 返回剩余数
        int totalNum = publishJobNum + publishNumByMember;
        long remainNum = totalNum - count;
        if (remainNum > 0) {
            return R.ok(remainNum);
        }
        return R.ok(0L);
    }

    @Override
    public Page<JobInfoEntity> selectCompanyJobPage(Page page, LambdaQueryWrapper<JobInfoEntity> wrapper) {
        return baseMapper.getCompanyJobPage(page, wrapper);
    }

    @Override
    public R updateJobInfo(JobInfoEntity jobInfo) {
        updateById(jobInfo);
        //根据职位状态判断是否需要插入职位发布记录
        if (jobInfo.getStatus().equals(JobStatusEnum.PUBLISHED)) {
            // 审核通过后插入
            UsageRecordsEntity usageRecordsEntity = new UsageRecordsEntity();
            usageRecordsEntity.setUsageType(UsageTypeEnum.JOB_PUBLISH);
            String name = SecurityUtils.getUser().getName();
            R<PlatformUserInfoEntity> platformedUser = remoteUserService.platformUser(name, SecurityConstants.FROM_IN);
            PlatformUserInfoEntity platformUserInfo = platformedUser.getData();
            if (platformUserInfo != null) {
                usageRecordsEntity.setUsageAccountId(platformUserInfo.getId());
            }
            usageRecordsEntity.setUsageCompanyId(jobInfo.getCompanyId());
            usageRecordsEntity.setServiceId(jobInfo.getId());
            usageRecordsEntity.setServiceType(UsageServiceTypeEnum.JOB_PUBLISH);
            usageRecordsEntity.setAmount(BigDecimal.ONE);
            usageRecordsEntity.setServiceName(jobInfo.getJobName());
            usageRecordsEntity.setUsageDesc(jobInfo.getCompanyName() + "发布职位:" + jobInfo.getJobName());
            remoteUsageRecordsService.add(usageRecordsEntity, SecurityConstants.FROM_IN);
        }
        return R.ok(Boolean.TRUE);
    }

    @Async
    @Scheduled(fixedRate = 60 * 1000)  // 每60秒执行一次
    public void refreshJobs() {
        log.info("查询待刷新职位信息...");
        // 获取当前时间的时间戳
        long now = System.currentTimeMillis();

        // 从有序集合中获取所有需要刷新的职位
        Set<ZSetOperations.TypedTuple<Object>> jobsToRefresh = redisTemplate.opsForZSet().rangeByScoreWithScores(JOB_REFRESH_KEY, 0, now);
        if (jobsToRefresh != null && !jobsToRefresh.isEmpty()) {
            log.info("待更新职位数量:{}", jobsToRefresh.size());
            for (ZSetOperations.TypedTuple<Object> tuple : jobsToRefresh) {
                log.info("待更新职位：{}", tuple.getValue());
                Long jobId = (Long) tuple.getValue();
                refreshJob(jobId, "AUTO");
            }

            // 移除已经刷新的职位
            redisTemplate.opsForZSet().removeRangeByScore(JOB_REFRESH_KEY, 0, now);
        }
    }

    // 刷新单个职位
    @Override
    public R refreshJob(Long jobId, String refreshType) {
        // 从数据库中获取职位信息
        JobInfoEntity job = getById(jobId);
        if (job == null) {
            return R.failed("未查询到职位信息");
        }
        // 1.查询今日刷新剩余数
        long count = autoRefreshLogService.count(new LambdaQueryWrapper<AutoRefreshLogEntity>()
                .eq(AutoRefreshLogEntity::getCompanyId, job.getCompanyId())
                .ge(AutoRefreshLogEntity::getCreatedAt, DateUtil.beginOfDay(new Date())));
        //根据用户名查询用户信息
        R<PlatformUserInfoEntity> platformedUser = remoteUserService.platformUser(job.getCompanyName(), SecurityConstants.FROM_IN);
        if (platformedUser.getCode() != CommonConstants.SUCCESS) {
            return R.failed(platformedUser.getMsg());
        }
        PlatformUserInfoEntity platformUserInfo = platformedUser.getData();
        //根据用户ID查询会员信息
        R<MemberInfoVO> accountInfo = memberInfoService.getAccountInfo(platformedUser.getData().getId(), SecurityConstants.FROM_IN);
        if (accountInfo.getCode() != CommonConstants.SUCCESS) {
            log.error("查询企业会员信息失败");
            return R.failed("查询企业会员信息失败");
        }
        MemberInfoVO infoVO = accountInfo.getData();
        if (infoVO == null) {
            return R.failed("未查询到会员权益信息");
        }
        if (!infoVO.getStatus().equals(MemberStatusEnum.ACTIVE)) {
            log.error("会员未激活");
            return R.failed("会员未激活");
        }
        MemberCompanyEquityConfEntity companyEquityConf = infoVO.getCompanyEquityConf();
        if (companyEquityConf == null) {
            log.error("未查询到企业会员权益信息");
            return R.failed("未查询到企业会员权益信息");
        }
        if (count == companyEquityConf.getJobRefreshDayNum() && !job.getUseECoinRefresh().equals("1")) {
            log.error("已达到职位日刷新上限");
            return R.failed("已达到职位日刷新上限");
        }
        //使用E币刷新:1.日累计刷新数大于等于日刷新数 2. 开启了E币刷新
        if (job.getUseECoinRefresh().equals("1") && count >= companyEquityConf.getJobRefreshDayNum()) {
            // 1. 判断账户E币余额
            String accountId;
            if (platformUserInfo.getAccountType().equals(PlatUserTypeEnum.MAIN)) {
                accountId = String.valueOf(platformUserInfo.getId());
            } else {
                // 取创建账号
                accountId = platformUserInfo.getCreateBy();
            }
            R<PlatformUserAccountEntity> account = remoteAccountService.getAccountInfo(accountId, SecurityConstants.FROM_IN);
            if (account.getCode() != CommonConstants.SUCCESS) {
                log.error(account.getMsg());
                return R.failed(account.getMsg());
            }
            BigDecimal eCoinNum = account.getData().getECoinNum();
            if (BigDecimal.ONE.compareTo(eCoinNum) > 0) {
                log.error("账户E币余额不足，职位刷新失败");
                return R.failed("账户E币余额不足，职位刷新失败");
            }
            // 2. 账户数据变更
            AccountChangeDTO accountChangeDTO = new AccountChangeDTO();
            accountChangeDTO.setAccountId(platformedUser.getData().getId());
            accountChangeDTO.setChangeMethod(ChangeMehtodEnum.SUBTRACT.name());
            accountChangeDTO.setChangeValue(BigDecimal.ONE);
            accountChangeDTO.setUsageType(UsageTypeEnum.E_COIN);
            R r = remoteAccountService.changeAccountInfo(accountChangeDTO, SecurityConstants.FROM_IN);
            if (r.getCode() != CommonConstants.SUCCESS) {
                return R.failed(r.getMsg());
            }
            // 3. 新增消费记录
            UsageRecordsEntity usageRecordsEntity = new UsageRecordsEntity();
            usageRecordsEntity.setUsageType(UsageTypeEnum.E_COIN);
            usageRecordsEntity.setUsageAccountId(platformedUser.getData().getId());
            usageRecordsEntity.setAmount(BigDecimal.ONE);
            usageRecordsEntity.setServiceId(job.getId());
            usageRecordsEntity.setServiceName(job.getJobName());
            usageRecordsEntity.setServiceType(UsageServiceTypeEnum.JOB_REFRESH);
            usageRecordsEntity.setUsageCompanyId(job.getCompanyId());
            usageRecordsEntity.setUsageItem(String.valueOf(job.getAutoRefreshDuration()));
            remoteUsageRecordsService.add(usageRecordsEntity, SecurityConstants.FROM_IN);
        }
        //  2. 新增刷新记录
        AutoRefreshLogEntity refreshLog = new AutoRefreshLogEntity();
        refreshLog.setCompanyId(job.getCompanyId());
        refreshLog.setJobId(jobId);
        refreshLog.setRefreshTarget("JOB");//职位
        refreshLog.setJobName(job.getJobName());
        refreshLog.setRefreshType(refreshType);//自动刷新
        autoRefreshLogService.save(refreshLog);
        // 3. 更新职位的刷新时间
        log.info("开始刷新职位ID:{}", jobId);
        job.setUpdateAt(LocalDateTime.now());
        updateById(job);

        // 4. 更新Redis中的刷新信息
        updateJobRefreshInfo(job);
        return R.ok();
    }

    @Override
    public R jobDetail(String jobId) {
        JobInfoEntity jobInfoEntity = baseMapper.selectById(jobId);
        JobDetailVO jobDetailVO = new JobDetailVO();
        if (jobInfoEntity == null) {
            return R.failed("未查询到简历信息");
        }
        R<PlatformUserInfoEntity> platformedUser = remoteUserService.platformUser(jobInfoEntity.getCompanyName(), SecurityConstants.FROM_IN);
        if (platformedUser.getCode() == CommonConstants.SUCCESS) {
            PlatformUserInfoEntity platformUserInfo = platformedUser.getData();
            jobDetailVO.setCompanyUserId(platformUserInfo.getId());
        }
        BeanUtil.copyProperties(jobInfoEntity, jobDetailVO);
        // 统计职位被查看数
        jobDetailVO.setJobCheckedNum(baseMapper.countJobCheckNum(jobId));
        jobDetailVO.setJobCollectNum(baseMapper.countJobCollectNum(jobId));
        jobDetailVO.setJobDeliveryNum(baseMapper.countJobDeliveryNum(jobId));
        jobDetailVO.setJobInviteNum(baseMapper.countJobInviteNum(jobId));
        jobDetailVO.setJobResumeNum(baseMapper.countMatchResumeNum(jobId));
        return R.ok(jobDetailVO);
    }

    @Override
    public R updateRefreshDuration(Integer refreshDuration, String jobId) {
        // 1. 查询待修改的职位
        JobInfoEntity jobInfo = this.getById(jobId);
        if (jobInfo == null) {
            return R.failed("未查询到职位信息");
        }
        if (refreshDuration == 0) {
            jobInfo.setAutoRefreshFlag("0");
            jobInfo.setAutoRefreshDuration(0);
            redisTemplate.opsForZSet().remove(JOB_REFRESH_KEY, jobInfo.getId());
            redisTemplate.opsForHash().delete(JOB_REFRESH_INFO_PREFIX + jobInfo.getId(), "last_refreshed_at");
            redisTemplate.opsForHash().delete(JOB_REFRESH_INFO_PREFIX + jobInfo.getId(), "refresh_interval");
        } else {
            jobInfo.setAutoRefreshDuration(refreshDuration);
            // 更新redis职位信息
            updateJobRefreshInfo(jobInfo);
        }
        updateById(jobInfo);
        return R.ok();
    }


    // 更新职位的刷新信息到Redis
    private void updateJobRefreshInfo(JobInfoEntity job) {
        log.info("开始更新redis职位信息,ID:{}", job.getId());
        // 计算下次刷新的时间戳
        LocalDateTime nextRefreshTime = job.getUpdateAt().plusHours(job.getAutoRefreshDuration());
        long nextRefreshTimestamp = nextRefreshTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

        // 更新职位的刷新信息
        redisTemplate.opsForHash().put(JOB_REFRESH_INFO_PREFIX + job.getId(), "last_refreshed_at", job.getUpdateAt());
        redisTemplate.opsForHash().put(JOB_REFRESH_INFO_PREFIX + job.getId(), "refresh_interval", job.getAutoRefreshDuration());

        // 将职位加入有序集合，按下次刷新时间排序
        redisTemplate.opsForZSet().add(JOB_REFRESH_KEY, job.getId(), nextRefreshTimestamp);
    }

    // 初始化职位的刷新信息到Redis
    public void initializeJobRefreshInfo(JobInfoEntity job) {
        updateJobRefreshInfo(job);
    }

    private static @NotNull UsageRecordsEntity getUsageRecords(JobTopVO jobTopVO, JobInfoEntity jobInfo) {
        UsageRecordsEntity usageRecordsEntity = new UsageRecordsEntity();
        usageRecordsEntity.setUsageType(UsageTypeEnum.E_COIN);
        usageRecordsEntity.setUsageAccountId(jobTopVO.getAccountId());
        usageRecordsEntity.setAmount(jobTopVO.getECoin());
        usageRecordsEntity.setServiceId(jobInfo.getId());
        usageRecordsEntity.setServiceName(jobInfo.getJobName());
        usageRecordsEntity.setServiceType(UsageServiceTypeEnum.TO_TOP);
        usageRecordsEntity.setUsageCompanyId(jobInfo.getCompanyId());
        usageRecordsEntity.setUsageItem(String.valueOf(jobTopVO.getTopDuration()));
        usageRecordsEntity.setUsageDesc("");
        return usageRecordsEntity;
    }
}
